using UnityEngine;
using System.Collections;

public class Tiro_Player : MonoBehaviour
{
    int id = 38;
    float[] vel_x = { 0.01f, -0.01f };
    float[] vel_y = { 0.02f, -0.02f };

    int r = Random.Range(0, 2);
    int ry = Random.Range(0, 2);

    bool active;

    void Start()
    {
        active = true;

        this.gameObject.AddComponent("MeshRenderer");
        this.gameObject.AddComponent("MeshFilter");
        this.gameObject.AddComponent("BoxCollider");
        this.gameObject.AddComponent("Rigidbody");
        this.gameObject.tag = "Player";
        this.gameObject.GetComponent<BoxCollider>().size = new Vector3(0.01f, 0.01f, 4f);
        this.gameObject.GetComponent<BoxCollider>().isTrigger = true;
        this.gameObject.transform.localPosition = new Vector3(0, 0, -1);
        this.gameObject.GetComponent<Rigidbody>().useGravity = false;
        this.gameObject.GetComponent<Rigidbody>().freezeRotation = true;

        Mesh mesh = Quad.Create(0.5f, 0.5f);

        this.gameObject.GetComponent<MeshFilter>().mesh = mesh;
        this.gameObject.GetComponent<MeshRenderer>().material = AtlasManager.globalMaterial;

        this.gameObject.GetComponent<MeshFilter>().mesh.uv = new Vector2[]
		{
			new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMax),
			new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMax),
			new Vector2(AtlasManager.rects[id].xMax, AtlasManager.rects[id].yMin),
			new Vector2(AtlasManager.rects[id].xMin, AtlasManager.rects[id].yMin)
		};

    }


    void Update()
    {
        //while (active)
        //{
        this.gameObject.transform.Translate(vel_x[r], vel_y[ry], 0);

        //    active = false;
        //}

        // Debug.Log("vel_x = " + vel_x + ",  vel_ y = " + vel_y);

        if (this.gameObject.transform.position.y >= 3.092476f)
        {
            vel_y[ry] *= -1;
        }
        if (this.gameObject.transform.position.y <= -2.584954f)
        {
            vel_y[ry] *= -1;
        }
        if (this.gameObject.transform.position.x >= 2.8878f)
        {
            vel_x[r] *= -1;
        }

        if (this.gameObject.transform.position.x <= -2.616378f)
        {
            vel_x[r] *= -1;
        }
    }

    void OnTriggerEnter(Collider c)
    {
        if (c.gameObject.tag == "Alvo10")
        {
            Tiro_Game.colidindo = true;
        }
    }
    void OnTriggerExit(Collider c)
    {
        if (c.gameObject.tag == "Alvo10")
        {
            Tiro_Game.colidindo = false;
        }
    }
}